package cn.just.controller;

import cn.just.entity.Article;
import cn.just.entity.Link;
import cn.just.entity.Tag;
import cn.just.entity.User;
import cn.just.service.ArticleService;
import cn.just.service.LinkService;
import cn.just.service.TagService;
import cn.just.utils.Page;
import cn.just.utils.THUtils;
import com.alibaba.fastjson.JSON;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wujiaming
 */
@WebServlet(name = "SearchServlet",value = "/SearchServlet")
public class SearchServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keywords = request.getParameter("keywords");
//        System.out.println(keywords);

        String pageNoStr = request.getParameter("pageNo");
        int pageNo = pageNoStr==null?1:Integer.parseInt(pageNoStr);
        Context context = new Context();
        context.setVariable("keywords",keywords);

        //将用户的信息填入页面中
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        context.setVariable("userName",user.getUserName());

        //提交的关键字回填到搜索框中
        context.setVariable("keywords",keywords);

        //查询相关的文章
        ArticleService articleService = new ArticleService();
        List<Article> list = articleService.findArticleByKeyWord(keywords);
        List<Article> pageList = null;
        int articleNumber = list.size();
//        System.out.println("articleNumber" + articleNumber);
        int maxPage = (articleNumber / 8) +1;
//        System.out.println("maxPage" +maxPage );
        int latPageNumArticle = articleNumber %8;
//        System.out.println("latPageNumArticle" + latPageNumArticle);
        if(pageNo<maxPage){
             pageList = list.subList((pageNo - 1) * 8, (pageNo - 1) * 8 + 8);
        }else{
             pageList = list.subList((pageNo - 1) * 8, (pageNo - 1) * 8 + latPageNumArticle);
        }


        //或者模糊查询文章的数量

        //将模糊查询得到的文章放入page结合中
        Page<Article> page = new Page<Article>();
        page.addAll(pageList);
        //设置page页面中的prePage，nextPage，totalPage
        page.setValue(pageNo,articleNumber,8);
        context.setVariable("articleList",page);

        //查询最新文章列表
        List<Article> newList = articleService.getNewList();
        context.setVariable("newList",newList);

        //查询评论最多的文章
        List<Article> maxCommentArticle = articleService.getMaxCommentArticle();
        context.setVariable("maxCommentArticle",maxCommentArticle);

        //查询评论最多的文章
        List<Article> maxViewArticle = articleService.getMaxViewArticle();
        context.setVariable("maxViewArticle",maxViewArticle);

        //查询标签没引用对多的
        TagService tagService = new TagService();
        List<Tag> tags = tagService.findTagByreferenceCount();
        context.setVariable("tags",tags);

        //友情链接,查询所有的链接名称和地址
        LinkService linkService = new LinkService();
        List<Link> links = linkService.findAll();
        context.setVariable("links",links);
        THUtils.write("blog/divlist",context,response);


//        Map<String, Object> resultMap = new HashMap<String, Object>();
//        resultMap.put("keywords",keywords);
//        String jsonString = JSON.toJSONString(resultMap);
//        System.out.println(jsonString);
//         // 把这个格式化后的json数据传递给页面
//        response.setContentType("application/json;charset=utf-8");
//         // 写数据到resp里面
//		PrintWriter writer = response.getWriter();
//		// 写操作
//		writer.write(jsonString);
//		// 清空缓存
//		writer.flush();
//		// 写关闭
//		writer.close();


    }
}
